﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CLibArtreve.bll;
using CLibArtreve.security;
using CLibArtreve.models;
using System.ComponentModel;

namespace WebAppArtreve.views.shared.components.arts
{
    public partial class WUCLikeArt : System.Web.UI.UserControl
    {
        private static Int64 _artId;
        private string _connectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
            }
        }
        private Int64? _userId
        {
            get
            {
                ArtreveIdentity identity = (ArtreveIdentity)Context.User.Identity;
                return identity.User.Id;
            }
        }
        private string _imgLikeUrl = "~/styles/assets/images/Love.png";
        private string _imgNoLikeUrl = "~/styles/assets/images/noLove.png";
        private string _lblLike = "You like this";
        private string _lblNoLike = "Like me";

        //PROPERTIES
        [DefaultValue(null), Bindable(true)]
        public Int64 ArtId
        {
            get
            {
                return _artId;
            }
            set
            {
                _artId = value;
                updateLabel();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //BUTTON (LIKE ART) CLICKED
        protected void likeArt_Click(object sender, EventArgs e)
        {
            if (isLiked())
            {
                DeleteLike();
            }
            else
            {
                AddLike();
            }

            updateLabel();
        }

        //ADD LIKES
        private void AddLike()
        {
            try
            {
                //1. BLL
                //-----------------------
                UserArtBLL bllUserArt = new UserArtBLL(_connectionString);

                //2. MODELS
                //----------------------- 
                UserArt userArt = new UserArt();

                //3. INSERT ART IN MODEL
                //-----------------------                
                userArt.Id = _userId;
                userArt.ArtId = _artId;

                //4. INSERT MODEL IN DB
                //-----------------------  
                bllUserArt.InsertLike(userArt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void DeleteLike()
        {
            try
            {
                UserArtBLL bllUserArt = new UserArtBLL(_connectionString);
                UserArt userArt = new UserArt();
                userArt.Id = _userId;
                userArt.ArtId = _artId;
                bllUserArt.DeleteUserArt(userArt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private Boolean isLiked()
        {
            try
            {
                UserArtBLL bllUserArt = new UserArtBLL(_connectionString);
                return bllUserArt.IsLikedByUser(_artId, _userId);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void updateLabel()
        {
            if (isLiked())
            {
                imgLike.ImageUrl = _imgLikeUrl;
                lblLike.Text = _lblLike;
                likeArt.CssClass = "button liked";
            }
            else
            {
                imgLike.ImageUrl = _imgNoLikeUrl;
                lblLike.Text = _lblNoLike;
                likeArt.CssClass = "button";
            }
        }
    }
}